package chances.epg.ui.taglib.html;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.lang.StringUtils;

import chances.epg.ui.taglib.layout.AbstractLayoutTag;
import chances.epg.ui.taglib.model.Border;
import chances.epg.ui.taglib.model.LinkModel;
import chances.epg.ui.taglib.model.Rect;
import chances.epg.ui.taglib.model.Widget;

public class LinkTag extends EpgBodyTagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6383301672999286284L;
	private String focusCursor;
	private String cursor;
	private String focusClass;

	private Rect rect;
	private String href;
	private String group;
	private String elementId;
	private boolean output = true;
	private Border borderRect;
	private String alt;
	private String title;
	private boolean disable;
	private Rect linkRect;
	private String zoomClass;
	private LinkModel linkModel = new LinkModel();

	
	public void setCode(String code) {
		linkModel.setCode(code);
	}

	public void setWidgetLink(Tag tag) {

		AbstractLayoutTag ancestorTag = findWidgetLayout(this);
		if (ancestorTag != null) {
			Widget widget = epgPageContext.getWidget(ancestorTag.getId());
			widget.addLink(this.getId());
		}
		ancestorTag = findGroupLayout(this);
		if (ancestorTag != null && StringUtils.isEmpty(this.group)) {
			this.group = ancestorTag.getGroup();
		}
	}

	public static AbstractLayoutTag findWidgetLayout(Tag tag) {
		AbstractLayoutTag ancestorTag = (AbstractLayoutTag) findAncestorWithClass(tag, AbstractLayoutTag.class);
		if (ancestorTag == null) {
			return null;
		} else if (StringUtils.isNotEmpty(ancestorTag.getWidget())) {
			return ancestorTag;
		} else {
			return findWidgetLayout(ancestorTag);
		}

	}

	public static AbstractLayoutTag findGroupLayout(Tag tag) {
		AbstractLayoutTag ancestorTag = (AbstractLayoutTag) findAncestorWithClass(tag, AbstractLayoutTag.class);
		if (ancestorTag == null) {
			return null;
		} else if (StringUtils.isNotEmpty(ancestorTag.getGroup())) {
			return ancestorTag;
		} else {
			return findGroupLayout(ancestorTag);
		}
	}

	@Override
	public void doAfterStart() throws JspException {
		this.setVm("link");
		super.doAfterStart();
		setWidgetLink(this);

	}

	@Override
	public int doEndTag() throws JspException {
		if (super.isDisplay()) {
			epgPageContext.addLink(getLinkModel(true));
		}
		return super.doEndTag();

	}

	public Rect getRect() {
		return rect;
	}

	@Override
	public void release() {
		this.focusCursor = null;
		this.borderRect = null;
		this.zoomClass = null;
		this.elementId = null;
		this.focusClass = null;
		this.href = null;
		this.rect = null;
		this.cursor = null;
		this.linkRect = null;
		this.alt = null;
		this.title = null;
		this.output = true;
		this.disable = false;
		this.group = null;
		this.linkModel = new LinkModel();
		super.release();
	}

	public void setRect(Rect rect) {
		this.rect = rect;
	}

	public String getFocusCursor() {
		return focusCursor;
	}

	public String getCursor() {
		return cursor;
	}

	public void setFocusCursor(String focusCursor) {
		this.focusCursor = focusCursor;
	}

	public void setCursor(String cursor) {
		this.cursor = cursor;
	}

	public String getFocusClass() {
		return focusClass;
	}

	public void setFocusClass(String focusClass) {
		this.focusClass = focusClass;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	protected Rect getOffset() {
		Rect result = new Rect();
		getOffset(result, this);
		return result;

	}

	protected void getOffset(Rect rect, Tag tag) {
		EpgRectTag epgRect = (EpgRectTag) findAncestorWithClass(tag, EpgRectTag.class);
		if (epgRect != null) {
			rect.add(epgRect.getRect());
			getOffset(rect, epgRect);
		}
	}

	public boolean isOutput() {
		return output;
	}

	public String getAlt() {
		return alt;
	}

	public String getTitle() {
		return title;
	}

	public void setAlt(String alt) {
		this.alt = alt;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setOutput(boolean output) {
		this.output = output;
	}

	public String getElementId() {
		return elementId;
	}

	public void setDisable(boolean disable) {
		this.disable = disable;
	}

	public void setElementId(String elementId) {
		this.elementId = elementId;
	}

	public String getZoomClass() {
		return zoomClass;
	}

	public void setZoomClass(String zoomClass) {
		this.zoomClass = zoomClass;
	}

	public LinkModel getLinkModel(boolean add) {
		Rect aRect = new Rect();
		Rect thisRect = this.getRect();
		if (thisRect != null) {
			thisRect.copyto(aRect);
		}

		if (add) {
			Rect offset = this.getOffset();
			aRect.setLeft(aRect.getLeft() + offset.getLeft());
			aRect.setTop(aRect.getTop() + offset.getTop());
		}

		this.linkRect = new Rect();
		thisRect.copyto(this.linkRect);
		linkRect.addBorder(this.borderRect);
		linkModel.setLinkRect(linkRect);
		linkModel.setRect(aRect);
		linkModel.setId(this.getId());
		linkModel.setHref(this.href);
		linkModel.setOnfocus(this.getOnfocus());
		linkModel.setOnblur(this.getOnblur());
		linkModel.setGroup(this.group);
		linkModel.setOnkeypress(this.getOnkeypress());
		linkModel.setAlt(this.alt);
		linkModel.setTitle(this.title);
		linkModel.setFocusCursor(this.focusCursor);
		linkModel.setDisable(this.disable);
		if(AbstractLayoutTag.class.isInstance(this.getParent())){
			AbstractLayoutTag parentTag = (AbstractLayoutTag) this.getParent();
			linkModel.setParentRect(parentTag.getRect());
		}else{
			linkModel.setParentRect(new Rect());
		}

		if (StringUtils.isNotEmpty(this.focusClass)) {
			linkModel.addEffect(this.getId(), String.format("css('%s')",this.focusClass));
		}
		if (StringUtils.isNotEmpty(this.zoomClass)) {
			linkModel.addEffect(this.getId(), String.format("zoom('%s')", this.zoomClass));
		}
		return this.linkModel;
	}
	public void addEffect(String id, String string) {
		linkModel.addEffect(id,string);
	}

	public void resetRect(Rect rect) {
		Rect aRect = new Rect();
		rect.copyto(aRect);

		this.rect = aRect;
	}

	public void setBorderStyle(String borderStyle) {
		this.borderRect = Border.parseStyle(borderStyle);
	}

	public void setBorder(int value) {
		this.borderRect = new Border(value);
	}

	public Rect getLinkRect() {
		return linkRect;
	}

	public void setLinkRect(Rect linkRect) {
		this.linkRect = linkRect;
	}

}
